import axios, { AxiosRequestConfig } from 'axios';

export interface MyResponseType<T> {
  code: number;
  message: string;
  data: T;
}
const instance = axios.create({
  //   baseURL: process.env.VUE_APP_BASE_API,
  baseURL: '/',
});

const request = async <T >(
  config: AxiosRequestConfig,
): Promise<MyResponseType<T>> => {
  try {
    const { data } = await instance.request<MyResponseType<T>>(config);
    // eslint-disable-next-line no-unused-expressions
    data.code === 0
      ? console.log(data.message) // 成功消息提示
      : console.error(data.message); // 失败消息提示
    return data;
  } catch (error) {
    console.error(error); // 失败消息提示
    return {
      code: -1,
      message: JSON.stringify(error),
      data: undefined as unknown as T,

    };
  }
};

export default request;
